package net.oschina.app.ui;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import net.oschina.app.AppManager;
import net.oschina.app.R;

/**
 * 应用程序Activity的基类
 * 
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-9-18
 */
public class BaseActivity extends ActionBarActivity {

	// 是否允许全屏
	private boolean allowFullScreen = true;

	// 是否允许销毁
	private boolean allowDestroy = true;

	private View view;

	// Basic layout for whole App to hold a navigation-drawer.
	private static final int NAVI_DRAWER_LAYOUT = R.layout.navi_drawer_container;

	// Use navigation-drawer for this fork.
	private ActionBarDrawerToggle mDrawerToggle;

	// Drawer.
	private DrawerLayout mDrawerLayout;

	// All content of all activities will be now added into drawer_content.
	private FrameLayout mDrawerContent;

	// Content of every activity.
	private View mActivityContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		allowFullScreen = true;
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);


	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(NAVI_DRAWER_LAYOUT);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerContent = (FrameLayout) findViewById(R.id.drawer_content);
		mActivityContent = getLayoutInflater().inflate(layoutResID, null);
		mDrawerContent.addView(mActivityContent, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		// Style ActionBar
		ActionBar actionBar = getSupportActionBar();
		if(actionBar != null) {
			actionBar.setHomeButtonEnabled(true);
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.widget_head_bg));
			mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, -1, -1);
			mDrawerLayout.setDrawerListener(mDrawerToggle);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mDrawerToggle != null) {
			mDrawerToggle.syncState();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// 结束Activity&从堆栈中移除
		AppManager.getAppManager().finishActivity(this);
	}

	public boolean isAllowFullScreen() {
		return allowFullScreen;
	}

	/**
	 * 设置是否可以全屏
	 * 
	 * @param allowFullScreen
	 */
	public void setAllowFullScreen(boolean allowFullScreen) {
		this.allowFullScreen = allowFullScreen;
	}

	public void setAllowDestroy(boolean allowDestroy) {
		this.allowDestroy = allowDestroy;
	}

	public void setAllowDestroy(boolean allowDestroy, View view) {
		this.allowDestroy = allowDestroy;
		this.view = view;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && view != null) {
			view.onKeyDown(keyCode, event);
			if (!allowDestroy) {
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
